<?php
require_once 'common/config.php';
require_once 'common/functions.php';

// 检查登录状态
check_login();

$db = db_connect();

// 获取搜索和筛选参数
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$status = isset($_GET['status']) ? intval($_GET['status']) : -1; // 默认状态为-1，表示全部
$date = isset($_GET['date']) ? trim($_GET['date']) : '';

// 分页参数
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$per_page = 10;
$offset = ($page - 1) * $per_page;

// 构建查询条件
$where = "b.type = 1"; // 只查询私教预约
$params = [];

if ($search) {
    $where .= " AND (u.name LIKE :search OR u.phone LIKE :search OR c.name LIKE :search)";
    $params[':search'] = "%{$search}%";
}

if ($status != -1) {
    $where .= " AND b.status = :status";
    $params[':status'] = $status;
}

if ($date) {
    $where .= " AND b.booking_date = :date";
    $params[':date'] = $date;
}

// 获取总记录数
$count_sql = "SELECT COUNT(*) FROM bookings b 
              LEFT JOIN users u ON b.user_id = u.id 
              LEFT JOIN coaches c ON b.coach_id = c.id 
              WHERE {$where}";
$count_stmt = $db->prepare($count_sql);
foreach ($params as $key => $value) {
    $count_stmt->bindValue($key, $value);
}
$count_stmt->execute();
$total_records = $count_stmt->fetchColumn();
$total_pages = ceil($total_records / $per_page);

// 获取预约记录
$sql = "SELECT b.*, c.name as coach_name, u.name as user_name, u.phone as user_phone 
        FROM bookings b 
        LEFT JOIN coaches c ON b.coach_id = c.id 
        LEFT JOIN users u ON b.user_id = u.id 
        WHERE {$where} 
        ORDER BY b.created_at DESC 
        LIMIT {$offset}, {$per_page}";

$stmt = $db->prepare($sql);
foreach ($params as $key => $value) {
    $stmt->bindValue($key, $value);
}
$stmt->execute();
$bookings = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 状态映射
$status_types = [
    0 => ['name' => '待确认', 'badge' => 'badge-warning'],
    1 => ['name' => '已确认', 'badge' => 'badge-info'],
    2 => ['name' => '已完成', 'badge' => 'badge-success'],
    3 => ['name' => '已取消', 'badge' => 'badge-danger']
];
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>私教预约管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>预约记录管理</h2>
                </div>
            </header>

            <div class="content">
                <!-- 搜索和筛选 -->
                <div class="filter-bar">
                    <form action="" method="GET" id="searchForm" class="search-bar">
                        <div class="form-group">
                            <input type="text" name="search" placeholder="搜索用户名、手机号、教练名..." 
                                   value="<?php echo htmlspecialchars($search); ?>">
                        </div>
                        <div class="form-group">
                            <select name="status" onchange="document.getElementById('searchForm').submit()">
                                <option value="-1">全部状态</option>
                                <?php foreach ($status_types as $key => $value): ?>
                                <option value="<?php echo $key; ?>" <?php echo $status === $key ? 'selected' : ''; ?>>
                                    <?php echo $value['name']; ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="form-group">
                            <input type="date" name="date" value="<?php echo $date; ?>" 
                                   onchange="document.getElementById('searchForm').submit()">
                        </div>
                        <button type="submit" class="btn-search">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                    </form>
                </div>

                <!-- 数据表格 -->
                <div class="data-table">
                    <table>
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>用户</th>
                                <th>教练</th>
                                <th>预约日期</th>
                                <th>时间段</th>
                                <th>价格</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($bookings)): ?>
                            <tr>
                                <td colspan="8" class="text-center">暂无数据</td>
                            </tr>
                            <?php else: ?>
                                <?php foreach ($bookings as $booking): ?>
                                <tr>
                                    <td><?php echo $booking['id']; ?></td>
                                    <td>
                                        <?php echo htmlspecialchars($booking['user_name']); ?>
                                        <div class="text-muted"><?php echo htmlspecialchars($booking['user_phone']); ?></div>
                                    </td>
                                    <td><?php echo htmlspecialchars($booking['coach_name']); ?></td>
                                    <td><?php echo $booking['booking_date']; ?></td>
                                    <td><?php echo htmlspecialchars($booking['time_slot']); ?></td>
                                    <td>¥<?php echo number_format($booking['price'], 2); ?></td>
                                    <td>
                                        <span class="badge <?php echo $status_types[$booking['status']]['badge']; ?>">
                                            <?php echo $status_types[$booking['status']]['name']; ?>
                                        </span>
                                    </td>
                                    <td>
                                        <a href="private_booking_detail.php?id=<?php echo $booking['id']; ?>" 
                                           class="btn-icon" title="查看详情">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                <?php if ($total_pages > 1): ?>
                <div class="pagination">
                    <?php 
                    $params = array_merge($_GET, ['page' => '']);
                    echo generate_pagination($page, $total_pages, $params);
                    ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <style>
    .filter-bar {
        background: #fff;
        padding: 20px;
        border-radius: 8px;
        height: 120px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        margin-bottom: 20px;
    }

    .search-bar {
        display: flex;
        gap: 15px;
        align-items: center;
        flex-wrap: wrap;
    }

    .form-group {
        flex: 1;
        min-width: 200px;
        margin-top: 20px;
    }

    .form-group input[type="text"],
    .form-group input[type="date"],
    .form-group select {
        width: 100%;
        padding: 10px 12px;
        border: 1px solid #ddd;
        border-radius: 4px;
        font-size: 14px;
        transition: all 0.3s;
        background: #f8f9fa;
    }

    .form-group input[type="text"]:focus,
    .form-group input[type="date"]:focus,
    .form-group select:focus {
        border-color: #3498db;
        box-shadow: 0 0 0 2px rgba(52,152,219,0.2);
        background: #fff;
    }

    .btn-search {
        padding: 10px 20px;
        background: #3498db;
        color: #fff;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        transition: background 0.3s;
        white-space: nowrap;
    }

    .btn-search:hover {
        background: #2980b9;
    }

    .badge {
        padding: 6px 12px;
        border-radius: 4px;
        font-size: 12px;
        font-weight: 500;
    }

    .badge-warning {
        background: #fff3cd;
        color: #856404;
        border: 1px solid #ffeeba;
    }

    .badge-info {
        background: #d1ecf1;
        color: #0c5460;
        border: 1px solid #bee5eb;
    }

    .badge-success {
        background: #d4edda;
        color: #155724;
        border: 1px solid #c3e6cb;
    }

    .badge-danger {
        background: #f8d7da;
        color: #721c24;
        border: 1px solid #f5c6cb;
    }

    .text-muted {
        color: #6c757d;
        font-size: 0.9em;
        margin-top: 2px;
    }

    .btn-icon {
        padding: 8px;
        border-radius: 4px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        color: #3498db;
        transition: all 0.3s;
    }

    .btn-icon:hover {
        background: rgba(52,152,219,0.1);
    }

    .data-table {
        background: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        overflow: hidden;
    }

    .data-table table {
        width: 100%;
        border-collapse: collapse;
    }

    .data-table th {
        background: #f8f9fa;
        padding: 12px 15px;
        text-align: left;
        font-weight: 600;
        color: #2c3e50;
        border-bottom: 2px solid #eee;
    }

    .data-table td {
        padding: 12px 15px;
        border-bottom: 1px solid #eee;
        vertical-align: middle;
    }

    .data-table tr:hover {
        background: #f8f9fa;
    }

    .text-center {
        text-align: center;
    }

    .pagination {
        display: flex;
        justify-content: center;
        margin-top: 20px;
        gap: 5px;
    }

    .pagination a,
    .pagination span {
        padding: 8px 12px;
        border: 1px solid #ddd;
        border-radius: 4px;
        color: #333;
        text-decoration: none;
        transition: all 0.3s;
    }

    .pagination a:hover {
        background: #f8f9fa;
        border-color: #3498db;
    }

    .pagination .current {
        background: #3498db;
        color: #fff;
        border-color: #3498db;
    }
    </style>
</body>
</html> 